package com.li.controller;

import com.li.pojo.News;
import com.li.pojo.NewsDto;
import com.li.pojo.ReturnMessage;
import com.li.pojo.WorkDto;
import com.li.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
public class NewsController {
    @Autowired
    private NewsService newsService;

    @RequestMapping("/selectNews")
    public ReturnMessage selectNews(@RequestParam Map<String, String> params){
        System.out.println("查询参数：");
        System.out.println(params);
        ReturnMessage returnMessage = new ReturnMessage();
        List<NewsDto> list = newsService.selectNews(params);
        long count = newsService.count(params);
        returnMessage.setData(list);
        returnMessage.setMsg("获取新闻数据成功");
        returnMessage.setOther(count);
        return returnMessage;
    }
    @RequestMapping("/updateNews")
    public ReturnMessage editNew(@RequestBody News news){
        ReturnMessage returnMessage = new ReturnMessage();
        System.out.println("更新:");
        System.out.println(news);
        newsService.updateNews(news);
        returnMessage.setMsg("新闻："+news.getName()+"更新成功!");
        returnMessage.setData(news);
        return returnMessage;
    }
    @RequestMapping("/addNews")
    public ReturnMessage  addNews(@RequestBody News news){
        ReturnMessage returnMessage = new ReturnMessage();
        newsService.addNews(news);
        returnMessage.setMsg(news.getName()+"添加成功!");
        returnMessage.setData(news);
        return returnMessage;
    }
    @RequestMapping("/deleteNews")
    public ReturnMessage deleteUser(@RequestBody List<Integer> deleteList){
        ReturnMessage returnMessage = new ReturnMessage();
        int f = newsService.deleteNews(deleteList);
        System.out.println("删除");
        System.out.println(deleteList);
        returnMessage.setData(deleteList);
        returnMessage.setMsg("删除成功");
        return returnMessage;
    }
    @RequestMapping("/editNews")
    public ReturnMessage editWork(NewsDto newsDto){
        ReturnMessage returnMessage = new ReturnMessage();
        newsService.editNews(newsDto);
        returnMessage.setMsg("更新作品成功");
        return returnMessage;
    }
}
